<template>
  <div>
    <page-dialog
      :title="title"
      v-model:visible="shows"
      :width="dialogInfo.width"
      :bt-list="dialogInfo.btList"
    >
      <page-form
        class="paddingBox order-model-formBox flex_wrap_start_start"
        v-model:ref-obj="formInfo.ref"
        :data="formInfo.data"
        :field-list="formInfo.fieldList"
        :rules="formInfo.rules"
        :label-width="formInfo.labelWidth"
        :list-type-info="listTypeInfo"
        @handleEvent="handleForm"
      >
        <template v-slot:form-assetName>
          <el-input
            v-model="formInfo.data.assetName"
            class="not-mongolia"
            type="input"
            :disabled="true"
            :placeholder="'请选择资产字典导入'"
          >
            <template #append>
              <el-icon @click="addEquipment"><MoreFilled /></el-icon>
            </template>
          </el-input>
        </template>
        <template v-slot:form-manageDeptName>
          <el-input
            v-model="formInfo.data.manageDeptName"
            class="not-mongolia"
            type="input"
            :disabled="true"
            :placeholder="'请选择部门'"
          >
            <template #append>
              <el-icon @click="checkManageDeptName"><MoreFilled /></el-icon>
            </template>
          </el-input>
        </template>
        <template v-slot:form-useUserName>
          <el-input
            v-model="formInfo.data.useUserName"
            class="not-mongolia"
            type="input"
            :disabled="true"
            :placeholder="'请选择使用人'"
          >
            <template #append>
              <el-icon @click="checkUseUserName"><MoreFilled /></el-icon>
            </template>
          </el-input>
        </template>
        <template v-slot:form-depotName>
          <el-input
            v-model="formInfo.data.depotName"
            type="input"
            class="not-mongolia"
            :disabled="true"
            placeholder="请选择仓库"
          >
            <template #append>
              <el-icon @click="openDepotWindow"><MoreFilled /></el-icon>
            </template>
          </el-input>
        </template>
        <template v-slot:form-supplierName>
          <el-input
            v-if="Number(formInfo.data.temporaryPurch) === 0"
            type="input"
            :disabled="true"
            :class="'not-mongolia'"
            placeholder="请选择供应商"
            v-model="formInfo.data.supplierName"
          >
            <template #append>
              <el-icon @click="openSupplierWindow"><MoreFilled /></el-icon>
            </template>
          </el-input>
          <el-input
            v-else
            v-model="formInfo.data.supplierName"
            placeholder="请选择供应商"
          />
        </template>

        <template v-slot:form-deviceTypeValue>
          <el-input
            v-model="formInfo.data.deviceTypeValue"
            type="input"
            :disabled="true"
            class="not-mongolia"
            placeholder="请选择医疗器械分类目录"
          >
            <template #append>
              <el-icon @click="openDeviceTypeWindow"><MoreFilled /></el-icon>
            </template>
          </el-input>
        </template>
        <template v-slot:form-classifyTypeValue>
          <el-input
            v-model="formInfo.data.classifyTypeValue"
            :class="'not-mongolia'"
            type="input"
            :disabled="true"
            placeholder="请选择资产分类"
          >
            <template #append>
              <el-icon @click="openAssetClassifyWindow"><MoreFilled /></el-icon>
            </template>
          </el-input>
        </template>
        <template v-slot:form-assetUseDeptBusinessVOList>
          <el-form
            :model="formInfo.data"
            ref="useDeptTableForm"
            style="width: 100%"
          >
            <dialog-table
              v-model:data="formInfo.data.assetUseDeptBusinessVOList"
              :field-list="assetUseDeptTableInfo.fieldList"
              :handle="assetUseDeptTableInfo.handle"
              :pager="false"
              :check-box="false"
              :tab-index="true"
              :summary="false"
              :maxHeight="200"
            >
              <template v-slot:col-useDeptName="scope">
                <el-form-item
                  style="min-width: 100% !important"
                  :prop="`assetUseDeptBusinessVOList.${scope.$index}.useDeptName`"
                  :rules="
                    scope.$index >= 0
                      ? [
                          {
                            required: true,
                            message: '请选择使用部门',
                            trigger: 'change',
                          },
                        ]
                      : []
                  "
                >
                  <el-input
                    style="width: 100%"
                    v-model="scope.row.useDeptName"
                    type="input"
                    :disabled="true"
                    class="not-mongolia"
                    placeholder="请选择使用部门"
                  >
                    <template #append>
                      <el-icon @click="openDept(scope.$index)"
                        ><MoreFilled
                      /></el-icon>
                    </template>
                  </el-input>
                </el-form-item>
              </template>
              <template v-slot:col-rate="scope">
                <el-form-item
                  style="min-width: 100% !important"
                  :prop="`assetUseDeptBusinessVOList.${scope.$index}.rate`"
                  :rules="
                    scope.$index >= 0
                      ? [
                          {
                            required: true,
                            message: '请输入占比',
                            trigger: 'blur',
                          },
                        ]
                      : []
                  "
                >
                  <el-input-number
                    style="width: 100%"
                    v-model.lazy="scope.row.rate"
                    clearable
                    controls-position="right"
                    :min="0"
                    :max="100"
                    :precision="2"
                  />
                </el-form-item>
              </template>
            </dialog-table>
          </el-form>
        </template>
        <template v-slot:form-assetCapitalSourceBusinessVOList>
          <el-form
            ref="assetCapitalSourceTableForm"
            :model="formInfo"
            style="width: 100%"
          >
            <dialog-table
              v-model:data="formInfo.data.assetCapitalSourceBusinessVOList"
              :pager="false"
              :check-box="false"
              :tab-index="true"
              :summary="true"
              :summary-field-list="assetCapitalSourceTableInfo.summaryFieldList"
              :field-list="assetCapitalSourceTableInfo.fieldList"
              :handle="assetCapitalSourceTableInfo.handle"
              :maxHeight="200"
            >
              <template v-slot:col-code="scope">
                <el-form-item
                  :prop="`data.assetCapitalSourceBusinessVOList.${scope.$index}.code`"
                  :rules="[
                    {
                      required: scope.$index >= 0,
                      message: '请选择资金来源',
                      trigger: ['change', 'blur'],
                    },
                  ]"
                >
                  <select-model
                    v-model.lazy="scope.row.code"
                    style="margintop: 18px"
                    :type-code="`asset_capital_source`"
                    :clearable="true"
                  />
                </el-form-item>
              </template>
              <template #col-amount="scope">
                <el-form-item
                  v-if="scope.$index >= 0"
                  :prop="`data.assetCapitalSourceBusinessVOList.${scope.$index}.amount`"
                  :rules="
                    scope.$index >= 0
                      ? [
                          {
                            validator: $validate.positiveMoneyZeroAsset,
                            trigger: ['blur', 'change'],
                          },
                        ]
                      : []
                  "
                >
                  <el-input-number
                    v-model.lazy="scope.row.amount"
                    style="width: 100%; margintop: 18px"
                    clearable
                    controls-position="right"
                    :precision="
                      $store.state.permission.systemConfig
                        .assets_price_precision
                    "
                    :min="0"
                    :max="999999999999999"
                    @change="changeAmount"
                  />
                </el-form-item>
              </template>
            </dialog-table>
          </el-form>
        </template>
      </page-form>
    </page-dialog>
    <SelectNoAuthUsers
      v-if="userInfo.visible"
      v-model:show-dialog="userInfo.visible"
      :api="userInfo.api"
      :save-empty="true"
      :check-box="false"
      :highlight-row="true"
      @callback="callBackUser"
    />
    <select-dept
      v-if="useDeptInfo.visible"
      v-model:show-dialog="useDeptInfo.visible"
      :check-box="false"
      :highlight-row="true"
      :query="useDeptInfo.query"
      :save-empty="true"
      @callback="callbackUseDept"
    />
    <select-dept
      v-if="manageDeptInfo.visible"
      v-model:show-dialog="manageDeptInfo.visible"
      :check-box="false"
      :highlight-row="true"
      :query="manageDeptInfo.query"
      :save-empty="true"
      @callback="callbackManageDept"
    />
    <SelectDepot
      v-if="selectDepotInfo.visible"
      v-model:show-dialog="selectDepotInfo.visible"
      :check-box="false"
      :highlight-row="true"
      :query="selectDepotInfo.query"
      :save-empty="true"
      @callback="selectDepotCallBack"
    />
    <select-supplier
      v-if="selectProductSupplier.visible"
      v-model:show-dialog="selectProductSupplier.visible"
      :query="selectProductSupplier.query"
      :check-box="false"
      :saveEmpty="true"
      :highlight-row="true"
      :api="selectProductSupplier.api"
      @callback="selectSupplierCallback"
    />
    <SelectSettingClassification
      v-if="selectSettingClassificationInfo.visible"
      v-model:showDialog="selectSettingClassificationInfo.visible"
      :saveEmpty="true"
      :check-box="false"
      :highlight-row="true"
      @callback="selectSettingClassificationInfoCallBack"
    />
    <SelectAssetClassify
      v-if="selectAssetClassifyInfo.visible"
      v-model:showDialog="selectAssetClassifyInfo.visible"
      :saveEmpty="true"
      :check-box="false"
      :highlight-row="true"
      @callback="selectAssetClassifyInfoCallBack"
    />
    <!-- 资产弹窗 -->
    <SelectEquipmentArchives
      v-if="equipmentInfo.visible"
      v-model:show-dialog="equipmentInfo.visible"
      :query="equipmentInfo.query"
      :check-box="false"
      :highlight-row="true"
      @callback="changeEquipment"
    />
  </div>
</template>
<script setup>
import { getUnitAll, getCoreCodeStatus, getSupplierPage } from '@/api/core'
import { getRrgUserList } from '@/api/base.js'
import _ from 'lodash'
const SelectNoAuthUsers = defineAsyncComponent(() =>
  import('@/components/SelectUsers/selectNoAuthUsers')
)
const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps({
  showDialog: {
    type: Boolean,
    default: false,
  },
  tableRow: {
    type: Object,
    default: function () {
      return {}
    },
  },
  title: {
    type: String,
    default: '新增盘盈',
  },
  inventoryLocation: {
    type: Number,
  },
})
const changeAmountFlag = ref(false)
const formRefs = ref([])
const listTypeInfo = ref({
  unitList: [],
})
const formInfo = ref({
  ref: null,
  data: {
    assetName: '',
    increaseType: 22,
    unitId: '',
    unitName: '',
    useDeptId: '',
    manageDeptName: '',
    manageDeptId: '',
    useUserName: '',
    useUser: '',
    useStatusValue: '',
    useStatus: '',
    location: '',
    standard: '',
    depotName: '',
    depotId: '',
    assetCapitalSourceBusinessVOList: [{ code: '', amount: 0 }],
    assetUseDeptBusinessVOList: [{ useDeptName: '' }],
    qty: 1,
    remark: '',
  },
  fieldList: [
    {
      label: '资产名称',
      value: 'assetName',
      type: 'slot',
    },
    {
      label: '供应商',
      value: 'supplierName',
      disabled: false,
      type: 'slot',
    },
    {
      label: '资产卡片号',
      value: 'assetCode',
      type: 'input',
      show: true,
    },
    {
      label: '规格',
      value: 'standard',
      type: 'input',
      disabled: true,
      show: true,
    },
    {
      label: '厂家名称',
      value: 'brandName',
      type: 'input',
      disabled: true,
      maxlength: 100,
    },
    {
      label: '品牌',
      value: 'mark',
      disabled: true,
      type: 'input',
      maxlength: 100,
    },
    {
      label: '医疗器械分类目录',
      value: 'deviceTypeValue',
      type: 'input',
      disabled: true,
    },
    {
      label: '临时采购',
      value: 'temporaryPurch',
      type: 'select-model',
      clearable: true,
      code: 'default_no_or_yes',
      event: 'temporaryPurch',
      disabled: false,
    },

    {
      label: '产地',
      value: 'originPlace',
      type: 'input',
    },
    {
      label: '使用状态',
      value: 'useStatus',
      type: 'select-model',
      code: 'asset_use_status',
      onlyShowOption: ['1', '2'],
      disabled: true,
    },
    // {
    //   label: '计提标志',
    //   value: 'accrualMark',
    //   type: 'select-model',
    //   clearable: true,
    //   code: 'accrual_mark',
    //   disabled: true,
    // },

    {
      label: '仓库',
      value: 'depotName',
      type: 'slot',
      show: false,
    },
    // {
    //   label: '管理部门',
    //   value: 'manageDeptName',
    //   type: 'slot',
    // },
    {
      label: '增加方式',
      value: 'increaseType',
      type: 'select-model',
      checkDefault: false,
      onlyShowOption: ['22'],
      code: 'increase_type',
      disabled: true,
    },
    {
      label: '计量单位',
      value: 'unitId',
      type: 'select',
      list: 'unitList',
      clearable: true,
      filterable: true,
      disabled: true,
    },
    {
      label: '数量',
      value: 'qty',
      type: 'number',
      min: 1,
    },
    {
      label: '原值',
      value: 'primitiveOriginalAmount',
      type: 'number',
      max: 999999999999999,
      formatter: proxy.$fn.TripartiteMethodAsset,
      disabled: true,
      precision:
        proxy.$store.state.permission.systemConfig.assets_price_precision,
      event: 'primitiveOriginalAmount',
    },
    {
      label: '资产分类',
      value: 'classifyTypeValue',
      type: 'input',
      disabled: true,
    },
    {
      label: '设备标识',
      value: 'assetMark',
      type: 'select-model',
      clearable: true,
      multiple: true,
      disabled: true,
      code: 'assets_mark',
    },
    {
      label: '使用人',
      value: 'useUserName',
      type: 'slot',
    },

    {
      label: '存放地点',
      value: 'location',
      type: 'input',
    },
    {
      label: '备注',
      value: 'remark',
      type: 'textarea',
      maxlength: 200,
    },
    {
      label: '使用部门',
      value: 'assetUseDeptBusinessVOList',
      type: 'slot',
      className: 'not-br',
    },
    {
      label: '资金来源',
      value: 'assetCapitalSourceBusinessVOList',
      type: 'slot',
      className: 'not-br',
      required: true,
    },
  ],
  rules: {
    assetName: [
      { required: true, message: '请选择资产字典导入', trigger: 'change' },
    ],
    qty: [{ required: true, message: '请输入数量', trigger: 'blur' }],
    // supplierName: [
    //   { required: true, message: '请选择供应商', trigger: 'change' },
    // ],
  },
  labelWidth: '100px',
})
const selectProductSupplier = ref({
  visible: false,
  api: getSupplierPage,
  query: {
    supplierAuth: 'supplierId',
  },
})
const selectSettingClassificationInfo = ref({
  visible: false,
  saveEmpty: true,
  query: {},
})
const selectAssetClassifyInfo = ref({
  visible: false,
  saveEmpty: true,
  query: {},
})
const userInfo = ref({
  visible: false,
  api: getRrgUserList,
})
const useDeptInfo = ref({
  visible: false,
  index: '',
  query: {
    status: 1,
    deptAuth: 'id',
    idNotIn: '',
  },
})
const manageDeptInfo = ref({
  visible: false,
  query: {
    deptAuth: 'id',
    status: 1,
  },
})
const selectDepotInfo = ref({
  visible: false,
  query: {
    depotAuth: 'id',
    status: 1,
    useTypeId: 2,
    isCargoSpaceManage: 0,
  },
})
const equipmentInfo = ref({
  visible: false,
  query: {
    // supplierId: {
    //   queryType: 'ALL_LIKE',
    //   value: '',
    // },
    id: {
      queryType: 'NOT_IN',
      value: '',
    },
  },
})
//资金来源表格
const assetCapitalSourceTableInfo = ref({
  data: [],
  summaryFieldList: [
    { value: 'amount', formatter: proxy.$fn.TripartiteMethodAsset },
  ],
  fieldList: [
    {
      label: '资金来源',
      value: 'code',
      type: 'slot',
    },
    {
      label: '金额',
      value: 'amount',
      type: 'slot',
    },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '100',
    btList: [
      {
        label: '新增行',
        event: addSource,
        show: true,
      },
      {
        label: '删除',
        event: removeSource,
        popconfirmText: '删除后将改变数据,是否继续？',
        show: true,
        ifRender: ifRenderRemoveSource,
      },
    ],
  },
})
const assetUseDeptTableInfo = ref({
  data: [],
  fieldList: [
    { label: '部门', value: 'useDeptName', type: 'slot' },
    {
      label: '占比(%)',
      value: 'rate',
      type: 'slot',
      labelTip: '总值100%',
    },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '100',
    btList: [
      {
        label: '新增行',
        event: addDept,
        show: true,
      },
      {
        label: '删除',
        event: removeDept,
        show: true,
        ifRender: ifRenderRemoveDept,
      },
    ],
  },
})
// 弹窗相关
const dialogInfo = ref({
  width: '960px',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      event: save,
      show: true,
    },
  ],
})
const assetCapitalSourceTableForm = ref()
const useDeptTableForm = ref()
const shows = computed({
  get() {
    getUnitList()
    getStatus()
    init()
    if (props.tableRow && Object.keys(props.tableRow).length) {
      setData()
    }
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})
watch(
  () => props.inventoryLocation,
  (val) => {
    if (val === 1) {
      //盘点位置为部门，增加方式默认为盘盈，不可编辑；使用状态为正常使用，不可编辑。
      //使用人，存放地点，计量单位改为非必填
      formInfo.value.data.useStatus = 2
      showFieldListItem(true, 'assetUseDeptBusinessVOList')
      showFieldListItem(false, 'depotName')
    } else if (val === 2) {
      //盘点位置为仓库，新增盘盈页字段显示。管理部门由仓库联动。标*为必填。
      //增加方式默认为盘盈，不可编辑。使用状态为待领用，不可编辑
      formInfo.value.data.useStatus = 1
      showFieldListItem(false, 'assetUseDeptBusinessVOList')
      showFieldListItem(true, 'depotName')
      showFieldListItem(false, 'useUserName')
    }
  },
  {
    immediate: true,
    deep: true,
  }
)
function init() {
  proxy.$nextTick(() => {
    formRefs.value = [
      formInfo.value.ref,
      assetCapitalSourceTableForm.value,
      useDeptTableForm.value,
    ]
  })
}

function addEquipment() {
  // if (!formInfo.value.data.supplierId || formInfo.value.data.supplierId == -2) {
  //   proxy.$message({
  //     showClose: true,
  //     message: '请选择供应商',
  //     type: 'warning',
  //     duration: 3500,
  //   })
  //   return
  // }
  // equipmentInfo.value.query.supplierId.value = formInfo.value.data.supplierId
  equipmentInfo.value.query.id.value = formInfo.value.data.productId
  equipmentInfo.value.visible = true
}
function changeEquipment(data) {
  if (data) {
    if (data.unitId !== -2) {
      formInfo.value.data.unitId = data.unitId
    } else {
      formInfo.value.data.unitId = ''
    }
    formInfo.value.data.brandName = data.brandName
    formInfo.value.data.standard = data.standard
    formInfo.value.data.deviceTypeValue = data.deviceTypeValue
    formInfo.value.data.deviceType = data.deviceType
    formInfo.value.data.supplierName = data.supplierName
    formInfo.value.data.supplierId = data.supplierId
    formInfo.value.data.mark = data.trademark
    formInfo.value.data.productId = data.id
    formInfo.value.data.assetName = data.commonName
    formInfo.value.data.assetMark = data.assetMark
    formInfo.value.data.assetMarkValue = data.assetMarkValue
    if (data.financeTypeValue) {
      formInfo.value.data.isFinanceAsset = 1
      formInfo.value.data.financialClassifyTypeValue = data.financeTypeValue
      formInfo.value.data.financialClassifyType = data.financeType
      formInfo.value.data.classifyTypeValue = data.assetTypeValue
      formInfo.value.data.classifyType = data.assetType
    } else {
      formInfo.value.data.isFinanceAsset = 0
      formInfo.value.data.financialClassifyType = data.financeType
      formInfo.value.data.financialClassifyTypeValue = data.financeTypeValue
      formInfo.value.data.classifyTypeValue = data.assetTypeValue
      formInfo.value.data.classifyType = data.assetType
    }
  }
}
function addSource() {
  //增加资金来源
  formInfo.value.data.assetCapitalSourceBusinessVOList.push({
    code: '',
    amount: 0,
  })
}
function removeSource(row) {
  //删除资金来源
  formInfo.value.data.assetCapitalSourceBusinessVOList =
    formInfo.value.data.assetCapitalSourceBusinessVOList.filter(
      (item) => item !== row
    )
  changeAmount()
  setTimeout(() => {
    assetCapitalSourceTableForm.value.clearValidate()
  }, 30)
}
function ifRenderRemoveSource() {
  return formInfo.value.data.assetCapitalSourceBusinessVOList.length - 1
}
function addDept() {
  formInfo.value.data.assetUseDeptBusinessVOList.push({
    useDeptName: '',
  })
}
function ifRenderRemoveDept() {
  return formInfo.value.data.assetUseDeptBusinessVOList.length - 1
}
function removeDept(row) {
  //删除使用部门
  formInfo.value.data.assetUseDeptBusinessVOList =
    formInfo.value.data.assetUseDeptBusinessVOList.filter(
      (item) => item !== row
    )
}
function openDept(index) {
  useDeptInfo.value.index = index
  let ids = formInfo.value.data.assetUseDeptBusinessVOList
    .filter((item) => item.useDeptId && item.useDeptId !== -2)
    .map((item) => item.useDeptId)
  useDeptInfo.value.query.idNotIn = ids && ids.length ? ids.toString() : ''
  useDeptInfo.value.visible = true
}
function callbackUseDept(data) {
  let index = useDeptInfo.value.index
  if (data && data.id) {
    formInfo.value.data.assetUseDeptBusinessVOList[index].useDeptName =
      data.name
    formInfo.value.data.assetUseDeptBusinessVOList[index].useDeptId = data.id
  } else {
    formInfo.value.data.assetUseDeptBusinessVOList[index].useDeptName = ''
    formInfo.value.data.assetUseDeptBusinessVOList[index].useDeptId = -2
  }
}
function openDepotWindow() {
  selectDepotInfo.value.visible = true
}
function selectDepotCallBack(row) {
  if (row && row.id) {
    formInfo.value.data.depotId = row.id
    formInfo.value.data.depotName = row.name
    formInfo.value.data.manageDeptId = row.deptId
    formInfo.value.data.manageDeptName = row.deptName
  } else {
    formInfo.value.data.depotId = -2
    formInfo.value.data.depotName = ''
    formInfo.value.data.manageDeptId = -2
    formInfo.value.data.manageDeptName = ''
  }
}
function changeAmount() {
  changeAmountFlag.value = true
  let primitiveOriginalAmount = 0
  if (
    formInfo.value.data.assetCapitalSourceBusinessVOList &&
    formInfo.value.data.assetCapitalSourceBusinessVOList.length
  ) {
    formInfo.value.data.assetCapitalSourceBusinessVOList.forEach((item) => {
      primitiveOriginalAmount = proxy.$fn.accAdd(
        item.amount || 0,
        primitiveOriginalAmount
      )
    })
  }
  formInfo.value.data.primitiveOriginalAmount = primitiveOriginalAmount
}
function handleForm(event) {
  if (event === 'primitiveOriginalAmount') {
    if (
      !changeAmountFlag.value &&
      formInfo.value.data.assetCapitalSourceBusinessVOList &&
      formInfo.value.data.assetCapitalSourceBusinessVOList.length === 1
    ) {
      formInfo.value.data.assetCapitalSourceBusinessVOList[
        formInfo.value.data.assetCapitalSourceBusinessVOList.length - 1
      ].amount = formInfo.value.data.primitiveOriginalAmount
    }
  } else if (event === 'temporaryPurch') {
    formInfo.value.data.supplierName = ''
    formInfo.value.data.supplierId = -2
  }
}

//获取计量单位列表
function getUnitList() {
  getUnitAll().then((resp) => {
    if (resp && resp.success && resp.data && resp.data.length > 0) {
      listTypeInfo.value.unitList = resp.data.map(function (item) {
        return { key: item.name, value: item.id }
      })
    }
  })
}
function getStatus() {
  getCoreCodeStatus({ codeType: '7' }).then((res) => {
    showFieldListItem(!res.data, 'assetCode')
  })
}
function showFieldListItem(show, nameValue) {
  formInfo.value.fieldList.forEach((item) => {
    if (nameValue === item.value) {
      item.show = show
    }
  })
}
function setData() {
  formInfo.value.data = props.tableRow
  formInfo.value.data.unitId =
    props.tableRow.unitId === -2 ? '' : props.tableRow.unitId
}
function checkUseUserName() {
  userInfo.value.visible = true
}

function checkManageDeptName() {
  manageDeptInfo.value.visible = true
}
function callBackUser(data) {
  if (data && data.id) {
    formInfo.value.data.useUserName = data.name
    formInfo.value.data.useUser = data.id
  } else {
    formInfo.value.data.useUserName = ''
    formInfo.value.data.useUser = -2
  }
}
function callbackManageDept(data) {
  if (data && data.id) {
    formInfo.value.data.manageDeptName = data.name
    formInfo.value.data.manageDeptId = data.id
  } else {
    formInfo.value.data.manageDeptName = ''
    formInfo.value.data.manageDeptId = -2
  }
}

function save() {
  proxy.$fn.checkForm(formRefs.value).then(() => {
    let arr = formInfo.value.data.assetCapitalSourceBusinessVOList
    let copyArr = proxy.$fn.deepClone(
      formInfo.value.data.assetCapitalSourceBusinessVOList
    )
    copyArr = copyArr.map((item) => {
      item.code = Number(item.code)
      return item
    })
    let uniqArr = _.uniqBy(copyArr, 'code')
    if (arr.length !== uniqArr.length) {
      proxy.$message({
        showClose: true,
        message: '一份资产卡片只能拥有一行相同的资金来源',
        type: 'warning',
        duration: 3500,
      })
      return
    }
    if (props.inventoryLocation === 2) {
      delete formInfo.value.data.assetUseDeptBusinessVOList
    } else if (props.inventoryLocation === 1) {
      let assetUseDeptBusinessVOList =
        formInfo.value.data.assetUseDeptBusinessVOList
      let sumNum = 0
      assetUseDeptBusinessVOList.forEach((item) => {
        sumNum = proxy.$fn.accAdd(item.rate || 0, sumNum)
      })
      if (sumNum > 100) {
        proxy.$message({
          showClose: true,
          message: '使用部门占比之后不能大于100%， 请修改',
          type: 'warning',
          duration: 3500,
        })
        return
      }
    }
    emits('callback', formInfo.value.data, () => {
      handleClose()
    })
  })
}
function openSupplierWindow() {
  selectProductSupplier.value.visible = true
}
function selectSupplierCallback(row) {
  if (row) {
    formInfo.value.data.supplierId = row.supplierId
    formInfo.value.data.supplierName = row.supplierName
  } else {
    formInfo.value.data.supplierId = ''
    formInfo.value.data.supplierName = ''
  }
}
function openDeviceTypeWindow() {
  selectSettingClassificationInfo.value.visible = true
}
function selectSettingClassificationInfoCallBack(row) {
  if (row) {
    formInfo.value.data.deviceType = row.id
    formInfo.value.data.deviceTypeValue = row.name
  } else {
    formInfo.value.data.deviceType = ''
    formInfo.value.data.deviceTypeValue = ''
  }
}
function openAssetClassifyWindow() {
  selectAssetClassifyInfo.value.visible = true
}
function selectAssetClassifyInfoCallBack(row) {
  if (row) {
    formInfo.value.data.classifyType = row.id
    formInfo.value.data.classifyTypeValue = row.name
  } else {
    formInfo.value.data.classifyType = '-2'
    formInfo.value.data.classifyTypeValue = ''
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
</script>

<style scoped lang="scss">
.i-more-disabled {
  cursor: not-allowed;
}
</style>
